package validadores;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
 *
 * @author Marcelo Gomes Neto
 */
public class ValidaConfirmarSenha implements Validator
{
    /*
    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {
    }
    */
    
    @Override
    public void validate(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException 
    {
        //Get the first password from the attribute.
        UIInput passwordComponent = (UIInput) toValidate.getAttributes().get("senha");
 
        //Get the value from the UIInput component.
        String password = (String) passwordComponent.getValue();
 
        //Get the value entered in the second string from the component parameter passed to the method.
        String confirm = (String) value;
 
        //Compare both fields.
        /*
        if(!password.equals(confirm)){
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Senha confirmada incorretamente!", null));
        }
        */
        if (password != null && password.trim().length() > 0  && !password.equals(confirm)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Senha confirmada incorretamente!", null));
	}
        
//        if (password.trim().length() < 6) {
//            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "A senha deve conter pelo menos 6 caracteres!", null));
//        }
    }
}